package demo.com.heq.service.impl;

import demo.com.heq.dao.DetailMapper;
import demo.com.heq.dao.UserMapper;
import demo.com.heq.entity.DetailEntity;
import demo.com.heq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author heq
 * @Date 2023/2/17 11:09
 * @Description 用户业务类实现
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    DetailMapper detailMapper;

    @Override
    public int getBalance(int id) {
        return userMapper.getBalance(id);
    }

    @Override
    public void consumption(int id, DetailEntity detailEntity) {
        int balance = this.getBalance(id); // 查询当前账户余额
        userMapper.modifyBalance(id, 100 + balance);
        detailMapper.addDetail(detailEntity);
    }

    @Override
    public void refund(int id, DetailEntity detailEntity) {
        int balance = this.getBalance(id); // 查询当前账户余额
        userMapper.modifyBalance(id, balance - 20);
        detailMapper.addDetail(detailEntity);
    }

    @Override
    public List<DetailEntity> getDetails(int id) {
        return detailMapper.getDetails(id);
    }
}
